package com.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.app.mapper.ApplyMapper;
import com.app.mapper.PersonMapper;
import com.app.model.OutgoingStatisticsModel;
import com.app.service.OutgoingStatisticsService;
import com.app.vo.LevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.app.common.ApplyTool.LEVEL_VO_LIST;


@Service
public class OutgoingStatisticsServiceImpl implements OutgoingStatisticsService {
    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public List<OutgoingStatisticsModel> getCountByDepartment(String startTime, String endTime) {
        if (StrUtil.isBlankIfStr(startTime) || StrUtil.isBlankIfStr(endTime)) {
            startTime = DateUtil.formatDateTime(new Date());
            endTime = DateUtil.formatDateTime(new Date());
        }
        List<OutgoingStatisticsModel> list = personMapper.getCountByDepartment(startTime, endTime);
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setNumber(i+1);
            if (list.get(i).getRealNumber() == 0) {
                list.get(i).setReignNumber(0);
            }
            if (list.get(i).getRealNumber() != 0) {
                DecimalFormat df = new DecimalFormat("#.#");
                double v = (double) list.get(i).getReignNumber() / list.get(i).getRealNumber() * 100;
                String formattedValue = df.format(v);
                list.get(i).setOccupancyRate( formattedValue+ "%");
            } else {
                list.get(i).setOccupancyRate(0 + "%");
            }

            if (list.get(i).getOutNumber() > 0) {
                String[] types = list.get(i).getLeaveTypes().split(",");
                for (String s : types
                ) {
                    Integer count = this.applyMapper.inquireAboutNumberLeaveList(list.get(i).getId(), Integer.parseInt(s), startTime, endTime);
                    if (count == null) {
                        count = 0;
                    }
                    switch (Integer.parseInt(s)) {
                        case 1:
                            list.get(i).setHolidayNumber(count);
                            break;
                        case 2:
                            list.get(i).setStudyNumber(count);
                            break;
                        case 3:
                            list.get(i).setAwayNumber(count);
                            break;
                        case 4:
                            list.get(i).setSeeDoctorNumber(count);
                            break;
                        case 5:
                            list.get(i).setSecondNumber(count);
                            break;
                        case 6:
                            list.get(i).setAbsenceNumber(count);
                            break;
                        case 7:
                            list.get(i).setTrainingNumber(count);
                            break;
                        case 8:
                            list.get(i).setJobTransferNumber(count);
                            break;
                        case 9:
                            list.get(i).setRetireNumber(count);
                    }
                }
            }
        }
        return list;

    }

    @Override
    public OutgoingStatisticsModel getCountTotal(String startTime, String endTime) {
        if (StrUtil.isBlankIfStr(startTime) || StrUtil.isBlankIfStr(endTime)) {
            startTime = DateUtil.formatDateTime(new Date());
            endTime = DateUtil.formatDateTime(new Date());
        }
        OutgoingStatisticsModel o = new OutgoingStatisticsModel();
        for (LevelVo levelVo : LEVEL_VO_LIST) {
            Integer count = this.applyMapper.inquireAboutNumberLeaveList(null, levelVo.getId(), startTime, endTime);
            if (count == null) {
                count = 0;
            }
            switch (levelVo.getId()) {
                case 1:
                    o.setHolidayNumber(count);
                    break;
                case 2:
                    o.setStudyNumber(count);
                    break;
                case 3:
                    o.setAwayNumber(count);
                    break;
                case 4:
                    o.setSeeDoctorNumber(count);
                    break;
                case 5:
                    o.setSecondNumber(count);
                    break;
                case 6:
                    o.setAbsenceNumber(count);
                    break;
                case 7:
                    o.setTrainingNumber(count);
                    break;
                case 8:
                    o.setJobTransferNumber(count);
                    break;
                case 9:
                    o.setRetireNumber(count);
            }
        }
        return o;
    }

    @Override
    public List<OutgoingStatisticsModel> getCountByPersonType(String startTime, String endTime) {
        if (StrUtil.isBlankIfStr(startTime) || StrUtil.isBlankIfStr(endTime)) {
            startTime = DateUtil.formatDateTime(new Date());
            endTime = DateUtil.formatDateTime(new Date());
        }
        List<OutgoingStatisticsModel> o = new ArrayList<>();
        List<OutgoingStatisticsModel> list = personMapper.getCountByPersonType(startTime, endTime);
        OutgoingStatisticsModel outgoingStatisticsModel1 = new OutgoingStatisticsModel();
        OutgoingStatisticsModel outgoingStatisticsModel2 = new OutgoingStatisticsModel();
        Integer total = 0;
        Integer cadre = 0;
        Integer soldier = 0;
        Integer total1 = 0;
        Integer cadre1 = 0;
        Integer soldier1 = 0;
        for (OutgoingStatisticsModel outgoingStatisticsModel : list) {
            total += outgoingStatisticsModel.getOutNumber();
            if (outgoingStatisticsModel.getPersonType() != null) {
                if (outgoingStatisticsModel.getPersonType() == 31) {
                    cadre = outgoingStatisticsModel.getOutNumber();
                }
                if (outgoingStatisticsModel.getPersonType() == 87) {
                    soldier = outgoingStatisticsModel.getOutNumber();
                }
            }
            outgoingStatisticsModel1.setTotal("在外：" + total);
            outgoingStatisticsModel1.setCadre("在外：" + cadre);
            outgoingStatisticsModel1.setSoldier("在外：" + soldier);

            total1 += outgoingStatisticsModel.getReignNumber();
            if (outgoingStatisticsModel.getPersonType() != null) {
                if (outgoingStatisticsModel.getPersonType() == 31) {
                    cadre1 = outgoingStatisticsModel.getReignNumber();
                }
                if (outgoingStatisticsModel.getPersonType() == 87) {
                    soldier1 = outgoingStatisticsModel.getReignNumber();
                }
            }
            outgoingStatisticsModel2.setTotal("在位：" + total1);
            outgoingStatisticsModel2.setCadre("在位：" + cadre1);
            outgoingStatisticsModel2.setSoldier("在位：" + soldier1);
        }
        o.add(outgoingStatisticsModel1);
        o.add(outgoingStatisticsModel2);

        return o;
    }
}
